//
//  RootViewController.m
//  Ringminder
//
//  Created by Think Small Studio on 09-6-20.
//  Copyright Think Small Studio 2009. All rights reserved.
//

#import "RootViewController.h"
#import "RingminderAppDelegate.h"
#import "EventViewController.h"


@implementation RootViewController
@synthesize eventTableView;
@synthesize appHelper;
@synthesize eventTimer;


- (void)viewDidLoad {
    [super viewDidLoad];
	
	// initialize the helper object
	appHelper = [[AppHelper alloc] init];
	
	// Set the title string
	self.title = [appHelper getTodayDateString:[NSDate date]];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
	
	// Set the right button as "+"
	UIBarButtonItem * btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
																		  target:self action:@selector(addEvent:)];
	
	// Start the timer
	eventTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onEventTimer) userInfo:nil repeats:YES];
	
	self.navigationItem.rightBarButtonItem = btn;
}

// Call this function when you hit the "+" button
- (void) addEvent:(id)sender {
	EventViewController *controller = [[EventViewController alloc] initWithNibName:@"EventViewController" bundle:nil];
	controller.editingItem = nil;
	[self.navigationController pushViewController:controller animated:YES];
	[controller release];
}

- (void)onEventTimer {
	
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	[self.eventTableView reloadData];
}

// Invoked when the user touches Edit.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    // Updates the appearance of the Edit|Done button as necessary.
    [super setEditing:editing animated:animated];
    [self.eventTableView setEditing:editing animated:YES];
    // Disable the add button while editing.
    if (editing) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    } else {
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	RingminderAppDelegate *appDelegate = (RingminderAppDelegate *)[[UIApplication sharedApplication] delegate];
    return [appDelegate.appData count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	RingminderAppDelegate *appDelegate = (RingminderAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSMutableDictionary *event = (NSMutableDictionary *)[appDelegate.appData objectAtIndex:indexPath.row];
	
	cell.textLabel.text = [event valueForKey:@"name"];
	NSMutableString *detailLabelText = [NSMutableString stringWithCapacity:0];
	[detailLabelText appendString:[appHelper getRepeatString:[event objectForKey:@"repeat"]]];
	[detailLabelText appendString:@" "];
	[detailLabelText appendString:[appHelper getTimeString:[event valueForKey:@"begintime"]]];
	[detailLabelText appendString:@" - "];
	[detailLabelText appendString:[appHelper getTimeString:[event valueForKey:@"endtime"]]];
	cell.detailTextLabel.text = detailLabelText;
	cell.detailTextLabel.textColor = [UIColor darkTextColor];
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	// cell.imageView.image = [UIImage imageNamed:@"bell.png"];

    return cell;
}




// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Navigation logic may go here -- for example, create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController animated:YES];
	// [anotherViewController release];
	[eventTableView deselectRowAtIndexPath:indexPath animated:NO];
	
	EventViewController *controller = [[EventViewController alloc] initWithNibName:@"EventViewController" bundle:nil];
	
	RingminderAppDelegate *appDelegate = (RingminderAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSMutableDictionary *event = (NSMutableDictionary *)[appDelegate.appData objectAtIndex:indexPath.row];
	
	controller.editingItem = event;
	[self.navigationController pushViewController:controller animated:YES];
	[controller release];	
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
		RingminderAppDelegate *appDelegate = (RingminderAppDelegate *)[[UIApplication sharedApplication] delegate];
		[appDelegate.appData removeObjectAtIndex:indexPath.row];
		
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}




// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
	RingminderAppDelegate *appDelegate = (RingminderAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	id object = [[appDelegate.appData objectAtIndex:fromIndexPath.row] retain];
	[appDelegate.appData removeObjectAtIndex:fromIndexPath.row];
	[appDelegate.appData insertObject:object atIndex:toIndexPath.row];
	[object release];
}



/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
	[eventTableView release];
	[appHelper release];
	[eventTimer release];
    [super dealloc];
}


@end

